package es.javamyadmin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.javamyadmin.dao.TablesDAO;
import es.javamyadmin.modelo.Tables;

/**
 * Servlet que muestra las tablas de una base de datos
 * @author Emilio Javier Castillo Gonzalez
 * @author Hector Delgado Duran
 *
 */
public class MostrarTablas extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3687937435943737303L;

	/**
	 * Metodo que llama a doGet
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * Metodo que genera una lista en html con las tablas de una base de datos
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String text = "<ul>";
		
		String user = request.getParameter("usuario");
		String pass = request.getParameter("pass");
		String host = request.getParameter("servidor");
		String port = request.getParameter("puerto");
		String database = request.getParameter("bd");
		TablesDAO listaTablas = new TablesDAO();
		listaTablas.setPath(getServletContext().getResourceAsStream("/WEB-INF/properties/mensajes.properties"));
		Iterator<Tables> lista = listaTablas.leerTables(user, pass, host, port, database).iterator();
		
		while(lista.hasNext()){
			Tables tablas = lista.next();
			text = text + "<li>" + tablas.getNombreTable() + "</li>";
		}
		
				
		text = text + "</ul>";
	    PrintWriter out = response.getWriter();
		try{
			out.print(text);
		}finally{
			out.close();
		}
	}

}
